﻿using System;
using System.Data;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using WeifenLuo.WinFormsUI.Docking;

namespace HRM
{
    public partial class frmLevelList : FormBase
    {
        int pageSize = UIParamater.pageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int LevelID;
        LevelClient wcfLevel;
        RoleRightInfo listRole;

        public override void Show_Form()
        {
            this.Show();
        }

        public frmLevelList()
        {
            InitializeComponent();
           // LoadLanguage();
            wcfLevel = new LevelClient(ProxyConfig.GetBinding(), ProxyConfig.Level);
            listRole = UIParamater.Quyen_Bac;
            if (!listRole._create)
                btnNew.Enabled = false;
            if (!listRole._delete)
                btnDel.Enabled = false;
            if (!listRole.Update)
            {
                this.btnEdit.Image = global::HRM.Properties.Resources.app;
                btnEdit.Text = "xem";
            }
            LoadData();
            this.KeyPreview = true;
        }
        void LoadLanguage()
        {
            if (this.Tag != null)
                this.Text = this.TabText = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
        }
        private void LoadData()
        {
            DataTable dt = wcfLevel.Level_GetList(out itemCount, currPage, pageSize).Tables[0];
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();

            dgvList.DataSource = dt;
            dgvList.Columns["Level_ID"].Visible = false;
            dgvList.Columns["Level_Name"].HeaderText = "Bậc";;
            dgvList.Columns["Level_Name"].Width = 200;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                Program.taskbarNotifier.Show(UpdateUI.GetString("canhbao"), UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                return;
            }
            Form f = new frmLevelInfo(0, listRole);
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                LoadData();
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (!listRole.Delete)
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("canhbao"), UpdateUI.GetString("khongcoquyenxoa"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    return;
                }
                LevelID = Convert.ToInt32(dgvList.CurrentRow.Cells["Level_ID"].Value);
                if (wcfLevel.Level_Using(LevelID) != 0)
                {
                    MessageBox.Show("Bậc này đang được sử dụng, không thể xóa!", UpdateUI.GetString("canhbao"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (MessageBox.Show(UpdateUI.GetString("bancochacchanxoa"), UpdateUI.GetString("canhbao"), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (wcfLevel.Level_Delete(LevelID))
                    {
                        Program.taskbarNotifier.Show(UpdateUI.GetString("canhbao"), UpdateUI.GetString("xoathanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        LoadData();
                    }
                    else
                    {
                        Program.taskbarNotifier.Show(UpdateUI.GetString("canhbao"), UpdateUI.GetString("xoakhongthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    }

                }
            }

        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (listRole.Update || listRole.View)
                {

                    LevelID = Convert.ToInt32(dgvList.CurrentRow.Cells["Level_ID"].Value);
                    Form f = new frmLevelInfo(LevelID, listRole);
                    if (f.ShowDialog(this) == DialogResult.OK)
                    {
                        LoadData();
                    }
                }
                else
                    Program.taskbarNotifier.Show(UpdateUI.GetString("canhbao"), UpdateUI.GetString("khongcoquyenxem"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
            }
        }

        private void btnFisrt_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage > 0 && currPage != totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage != 0)
                currPage--;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage != 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
                btnEdit_Click(null, null);
        }

        private void frmLevelList_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnEdit_Click(null, null);
                    break;
                case Keys.Delete:
                    btnDel_Click(null, null);
                    break;
            }
        }

        private void frmLevelList_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void frmLevelList_Activated(object sender, EventArgs e)
        {
            //LoadLanguage();
            if (dgvList.Columns.Count > 0)
            {
                dgvList.Columns["Level_Name"].HeaderText = "Bậc";
            }
        }
    }
}

